package com.itfreer.data.db;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itfreer.form.api.PageInfo;
import com.itfreer.utils.ResponseEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/data")
@Api("数据服务")
public class RestDataService {

	@Autowired(required = false)
	private DataService service;

	protected DataService getService() {
		return service;
	}

	@ApiOperation(value = "获取数据")
	@ApiImplicitParams(value = { @ApiImplicitParam(name = "id", value = "注册ID", paramType = "path", required = true),
			@ApiImplicitParam(name = "pInfo", value = "查询条件", paramType = "body", required = true) })
	@PostMapping(value = "/getRows/{id:.+}", produces = "application/json")
	public ResponseEntity<List<Map<String, Object>>> getRows(@PathVariable("id") String id, @RequestBody PageInfo pInfo)
			throws IOException {
		return new ResponseEntity<>(getService().getRows(id, pInfo));
	}

	@ApiOperation(value = "获取数据记录数")
	@ApiImplicitParams(value = { @ApiImplicitParam(name = "id", value = "注册ID", paramType = "path", required = true),
			@ApiImplicitParam(name = "where", value = "查询条件", paramType = "body", required = true) })
	@PostMapping(value = "/getRowCounts/{id:.+}", produces = "application/json")
	public ResponseEntity<Long> getRowCounts(@PathVariable("id") String id, @RequestBody Map<String, Object> where) throws IOException {
		return new ResponseEntity<>(getService().getRowCounts(id, where));
	}
}
